package cn.bugstack.domain.strategy.service.rule.tree.impl;

import cn.bugstack.domain.strategy.model.valobj.RuleLogicCheckTypeVO;
import cn.bugstack.domain.strategy.repository.IStrategyRepository;
import cn.bugstack.domain.strategy.service.rule.tree.ILogicTreeNode;
import cn.bugstack.domain.strategy.service.rule.tree.factory.DefaultTreeFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 次数锁节点
 */
@Slf4j
@Component("rule_lock")
public class RuleLockLogicTreeNode implements ILogicTreeNode {
    @Resource
    private IStrategyRepository repository;
    // 用户抽奖次数，后续完成这部分流程开发的时候，从数据库/Redis中读取
//    private Long userRaffleCount = 0L;


    @Override
    public DefaultTreeFactory.TreeActionEntity logic(String userId, Long strategyId, Integer awardId, String ruleValue, Date endDateTime) {
//        String rule_model = "rule_lock";
//        // 查询规则值配置；当前奖品ID， 抽奖中规则对应的校验值。如；1、2、6
//        String ruleValue = repository.queryStrategyRuleValue(strategyId, awardId, rule_model);
//        long raffleCount = Long.parseLong(ruleValue);
//        todo 为什么ruleValue有次数的意思？

//        log.info("规则过滤-次数锁 userId:{} strategyId:{} ruleModel:{}", userId, strategyId, awardId);
//        long raffleCount = 0L;
//        try {
//            raffleCount = Long.parseLong(ruleValue);
//        } catch (Exception e) {
//            throw new RuntimeException("规则过滤-次数锁异常 ruleValue: " + ruleValue + " 配置不正确");
//        }
//
////        查询用户抽奖次数 - 当天的；策略ID 1：1的配置，可以直接用strategyId查询
//        Integer userRaffleCount = repository.queryTodayUserRaffleCount(userId, strategyId);
//
//        // 用户抽奖次数大于规则限定值，规则放行
//        if (userRaffleCount >= raffleCount) {
//            log.info("规则过滤-次数锁【放行】 userId:{} strategyId:{} awardId:{} raffleCount:{} userRaffleCount:{}", userId, strategyId, awardId, userRaffleCount, userRaffleCount);
//            return DefaultTreeFactory.TreeActionEntity.builder()
//                    .ruleLogicCheckTypeVO(RuleLogicCheckTypeVO.ALLOW)
//                    .build();
//        }
//        log.info("规则过滤-次数锁【拦截】 userId:{} strategyId:{} awardId:{} raffleCount:{} userRaffleCount:{}", userId, strategyId, awardId, userRaffleCount, userRaffleCount);
//        // 用户抽奖次数小于规则限定值，规则拦截
//        return DefaultTreeFactory.TreeActionEntity.builder()
//                .ruleLogicCheckTypeVO(RuleLogicCheckTypeVO.TAKE_OVER)
//                .build();
//    }
        log.info("规则过滤-次数锁 userId:{} strategyId:{} awardId:{}", userId, strategyId, awardId);

        long raffleCount = 0L;
        try {
            raffleCount = Long.parseLong(ruleValue);
        } catch (Exception e) {
            throw new RuntimeException("规则过滤-次数锁异常 ruleValue: " + ruleValue + " 配置不正确");
        }

        // 查询用户抽奖次数 - 当天的；策略ID:活动ID 1:1 的配置，可以直接用 strategyId 查询。
        Integer userRaffleCount = repository.queryTodayUserRaffleCount(userId, strategyId);

        // 用户抽奖次数大于规则限定值，规则放行
        if (userRaffleCount > raffleCount) {
            log.info("规则过滤-次数锁【放行】 userId:{} strategyId:{} awardId:{} raffleCount:{} userRaffleCount:{}", userId, strategyId, awardId, userRaffleCount, userRaffleCount);
            return DefaultTreeFactory.TreeActionEntity.builder()
                    .ruleLogicCheckType(RuleLogicCheckTypeVO.ALLOW)
                    .build();
        }

        log.info("规则过滤-次数锁【拦截】 userId:{} strategyId:{} awardId:{} raffleCount:{} userRaffleCount:{}", userId, strategyId, awardId, userRaffleCount, userRaffleCount);

        // 用户抽奖次数小于规则限定值，规则拦截
        return DefaultTreeFactory.TreeActionEntity.builder()
                .ruleLogicCheckType(RuleLogicCheckTypeVO.TAKE_OVER)
                .build();
    }
}
